package com.API.dal.providers;

import com.API.dal.entities.*;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class ProjectProviderImp  implements ProjectProvider{

    @Autowired
    SessionFactory sessionFactory;

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addProject(Objects project) {
        ObjectsTypes ot;
        Session session  = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ObjectsTypes ot where ot.name = 'Проект'");
        ot = (ObjectsTypes) query.uniqueResult();
        project.setObjectsTypes(ot);
        session.save(project);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateProject(Objects project) {
        Session session = sessionFactory.getCurrentSession();
        session.update(project);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addProjectCustomer(ProjectCustomerId projectCustomerId, Objects project, Objects customer) {
        Session session = sessionFactory.getCurrentSession();
        ProjectCustomer pc = new ProjectCustomer(projectCustomerId, customer, project);
        session.save(pc);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addProjectCost(ProjectsCosts projectsCosts) {
        Session session = sessionFactory.getCurrentSession();
        session.save(projectsCosts);
    }


    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getProjects() {
            List<Objects> supplierList;
            Session session =sessionFactory.getCurrentSession();
            Query query = session.createQuery("from Objects ob where ob.objectsTypes.name = 'Проект'" );
            supplierList = query.list();
            return supplierList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public ProjectCustomer getProjectData(int project_id) {
        ProjectCustomer projectCustomer;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ProjectCustomer pc where pc.objectsByProjectId.id = " + project_id);
        projectCustomer = (ProjectCustomer) query.uniqueResult();
        return projectCustomer;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public ProjectCustomer getProjectData(String project_name) {
        ProjectCustomer projectCustomer;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ProjectCustomer pc where pc.objectsByProjectId.name = '" + project_name + "'");
        projectCustomer = (ProjectCustomer) query.uniqueResult();
        return projectCustomer;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public Objects getProjectCustomer(int project_id) {
        Objects customer;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select ob from Objects ob, ProjectCustomer pc " +
                "where ob.id = pc.objectsByCustomerId.id and  pc.objectsByProjectId.id =  " + project_id);
        customer = (Objects) query.uniqueResult();
        return customer;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public Objects getProjectCustomer(String project_name) {
        Objects customer;
        ProjectCustomer pc;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(" from ProjectCustomer pc " +
                "where pc.objectsByProjectId.name =  '" + project_name + "'");
        pc = (ProjectCustomer) query.uniqueResult();
        query = session.createQuery("from Objects o where o.id = " + pc.getId());
        customer = (Objects) query.uniqueResult();
        return customer;
    }


    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Incomes> getProjectIncomes(int project_id) {
        List<Incomes> incomesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select inc from Incomes inc, IncomesCustomersProjects icp" +
                " where inc.id = icp.incomes.id and icp.objectsByProjectId = " + project_id);
        incomesList = query.list();
        return incomesList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Incomes> getProjectIncomes(String project_name) {
        List<Incomes> incomesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select inc from Incomes inc, IncomesCustomersProjects icp" +
                " where inc.id = icp.incomes.id and icp.objectsByProjectId = '" + project_name + "'");
        incomesList = query.list();
        return incomesList;
    }


    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getProjectOrders(int project_id) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select ord  from Orders ord " +
                " where ord.objectsByProjectId.id = " + project_id);
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getProjectOrders(String project_name) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select ord  from Orders ord " +
                " where ord.objectsByProjectId.name= '" + project_name + "'");
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public Objects getProjectById(int project_id) {
        Objects project;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob where ob.id = " + project_id);
        project = (Objects) query.uniqueResult();
        return project;

    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public Objects getProjectByName(String project_name) {
        Objects project;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob where ob.name = '" + project_name + "'");
        project = (Objects) query.uniqueResult();
        return project;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Equipments> getProjectMaterials(int project_id) {
        List<Equipments> equipmentsList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select eq from Equipments eq, EquipmentsProjects ep " +
                " where eq.equipmentsTypes.id = 0 and eq.id = ep.equipments.id and ep.objects.id = " + project_id +
                " ");
        equipmentsList = query.list();
        return equipmentsList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Equipments> getProjectMechanisms(int project_id) {
        List<Equipments> equipmentsList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select eq from Equipments eq, EquipmentsProjects ep " +
                " where eq.equipmentsTypes.id = 3 and eq.id = ep.equipments.id and ep.objects.id = " + project_id);
        equipmentsList = query.list();
        return equipmentsList;    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<EquipmentsProjects> getEquipmentTotal(int project_id, int equipment_id) {
        List<EquipmentsProjects> equipmentsList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from EquipmentsProjects ep where  ep.objects.id = " + project_id + "" +
                " and ep.equipments.id = " + equipment_id);
        equipmentsList =  query.list();

        return equipmentsList;
    }
}
